package cn.com.lyb.core.response;

import cn.com.lyb.utils.StringUtils;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.Serializable;
import java.util.List;

public class ResultDTO<T> implements Serializable {

    private static final String SUCCESS_CODE = "200";
    private static final String FAILURE_CODE = "500";

    private String code;
    private T data;
    private String message;
    protected Integer total;

    public static <T> ResultDTO<T> success(T data) {
        return new ResultDTO(SUCCESS_CODE, data, StringUtils.EMPTY);
    }

    public static ResultDTO success() {
        return new ResultDTO(SUCCESS_CODE, StringUtils.EMPTY);
    }

    public static <T> ResultDTO<List<T>> success(PageInfo<T> pageInfo) {
        return new ResultDTO<>(SUCCESS_CODE, pageInfo.getList(), StringUtils.EMPTY, (int) pageInfo.getTotal());
    }

    public static <T> ResultDTO<T> failed(String message) {
        return new ResultDTO(FAILURE_CODE, message);
    }

    public static <T> ResultDTO<T> failed(String code, String message) {
        return new ResultDTO(code, message);
    }


    public static <T> ResultDTO<T> failed(Throwable t) {
        return failed(ExceptionUtils.getStackTrace(t));
    }

    private ResultDTO(){}

    public ResultDTO(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultDTO(String code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public ResultDTO(String code, T data, String message, Integer total) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.total = total;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }
}

